package com.smec.remes.module.search.adapter;

import android.content.Context;
import android.databinding.DataBindingUtil;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.hwangjr.rxbus.Bus;
import com.smec.moblie.BR;
import com.smec.moblie.R;
import com.smec.moblie.databinding.ItemPopuBinding;
import com.smec.remes.base.SmecBaseRecyclerViewHolder;
import com.smec.remes.bean.AbleMatnr;
import com.smec.remes.util.CommonUtils;

import java.util.ArrayList;

import static com.smec.remes.module.search.SmecSearchActivity.BUY_NULL;

/**
 * Created by Administrator on 2017/12/4.
 */

public class SmecBuyAdapter extends RecyclerView.Adapter<SmecBaseRecyclerViewHolder>  {

    private ArrayList<AbleMatnr> arrayList=new ArrayList<>();
    private Context mcontext;
    private Bus mRxbus;

    public SmecBuyAdapter(Context context,Bus smecRxBus) {
        this.mcontext=context;
        this.mRxbus=smecRxBus;
    }

    public void setArrayList(ArrayList<AbleMatnr> list){
        if(CommonUtils.notEmpty(list)){
            arrayList=list;
            notifyDataSetChanged();
        }
    }

    public ArrayList<AbleMatnr> _getList(){
        return arrayList;
    }

    @Override
    public SmecBaseRecyclerViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        ItemPopuBinding itemPopuBinding= DataBindingUtil.inflate
                (LayoutInflater.from(parent.getContext()), R.layout.item_popu,parent,false);
        SmecBaseRecyclerViewHolder smecBaseRecyclerViewHolder=new
                SmecBaseRecyclerViewHolder(itemPopuBinding.getRoot());
        smecBaseRecyclerViewHolder.setViewType(viewType);
        smecBaseRecyclerViewHolder.setBinding(itemPopuBinding);
        return smecBaseRecyclerViewHolder;
    }

    @Override
    public void onBindViewHolder(SmecBaseRecyclerViewHolder holder, final int position) {
        final ItemPopuBinding itemPopuBinding= (ItemPopuBinding) holder.getBinding();
        itemPopuBinding.setVariable(BR.ableMatnrBean,arrayList.get(position));

        itemPopuBinding.setIndex(position);
        itemPopuBinding.smecBuyDelete.setOnClickListener(new View.OnClickListener() {
            @Override
                public void onClick(View v) {
                int num=0;
                if(arrayList.get(position)!=null && !arrayList.get(position).getNum().toString().equals("")){
                    num= Integer.parseInt(arrayList.get(position).getNum());
                }else {
                    arrayList.get(position).setNum("0");
                }
                num=num-1;
                if(num <= 0){
                    arrayList.remove(position);
                    notifyItemRemoved(position);
                    notifyItemRangeChanged(0,arrayList.size());

                    if(arrayList.size() == 0){
                        mRxbus.post(BUY_NULL,new Object());
                    }

                }else{
                    itemPopuBinding.tvNum.setText(String.valueOf(num));
                    arrayList.get(position).setNum(String.valueOf(num));
                }
                notifyDataSetChanged();
            }
        });
        itemPopuBinding.smecBuyAdd.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                int num=0;
                if(arrayList.get(position)!=null && !arrayList.get(position).getNum().toString().equals("")){
                    num= Integer.parseInt(arrayList.get(position).getNum());
                }else {
                    arrayList.get(position).setNum("0");
                }
                num=num+1;
                itemPopuBinding.tvNum.setText(String.valueOf(num));
                arrayList.get(position).setNum(String.valueOf(num));
                notifyDataSetChanged();
            }
        });
        holder.getBinding().executePendingBindings();
    }

    @Override
    public int getItemCount() {
        return arrayList.size();
    }

}
